package com.zktr.dao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.zktr.pojo.OrderInfo;
import com.zktr.utils.CRUDDAO;

/**
 * 订单详情dao:
 * @author 123
 * @param <mediumint>
 * @param <tinyint>
 * @param <decimal>
 *
 */
public class OrderInfoDao {
	CRUDDAO<OrderInfo> crud=new CRUDDAO<>(OrderInfo.class);
	/**
	 * 测试通过
	 */
	public static void main(String[] args) {
		int count = dataCount("2018-08-20");
	    System.out.println(count+1);
/*		int sum = dao.selectOrderinfoCount();
		Map<Integer, List<OrderInfo>> selectOrders =dao.selectOrders(1, 3);
		List<OrderInfo> list = selectOrders.get(0);
		for (OrderInfo orders : list) {
			System.out.println(orders);
		}*/
		/*OrderInfo info=new OrderInfo<>();
		info.setOrderid("2");
		info.setOrderstatus("0");
		info.setConsignne("李四");
		int sum = dao.selectOrderinfoCount(info);
		Map<Integer, List<OrderInfo>> selectOrders =dao.selectOrders(info,2, 3);
		List<OrderInfo> list = selectOrders.get(0);
		for (OrderInfo orders : list) {
			System.out.println(orders);
		}*/
		
	}


	
	
	
	/**
	 * 分页查询总记录条数：
	 * @return 
	 * 
	 */
	public int  selectOrderinfoCount() {
		return crud.selectCount("select count(*) from order_info");
	}
	/**
	 * 分页查询所有订单：
	 *  pc:当前页
	 * ps:页大小
	 * @return 
	 */
	public Map<Integer, List<OrderInfo>> selectOrders(int pc,int ps) {
		Map<String,Object []> map=new LinkedHashMap<String,Object []> ();
		map.put("select * from order_info LIMIT ?,?",new Object[]{(pc-1)*ps,ps});
		return 	crud.selectAll(map);
	}
	/**
	 * 带条件的分页查询
	 * @param info
	 * @param pc
	 * @param ps
	 * @return
	 */
	public Map<Integer, List<OrderInfo>> selectOrders(OrderInfo info, int pc, int ps) {
		try {
			Map<String,Object []> map=new LinkedHashMap<String,Object []> ();
			StringBuilder sql = new StringBuilder("select * from order_info");
			StringBuilder limitSql = new StringBuilder("limit ?,?");
			List<Object> params = sqlCondition(sql,info);
			params.add((pc-1)*ps);
			params.add(ps);
			sql.append(limitSql);
			String newsql=sql.toString();
			map.put(newsql,params.toArray());
			return crud.selectAll(map);
		} catch(Exception e) {
			throw new RuntimeException(e);
		}
	}
	/**
	 * 带条件的查询总数
	 * @param info
	 * @return
	 */
	public int selectOrderinfoCount(OrderInfo info) {
		StringBuilder cntSql = new StringBuilder("select count(*) from order_info");
		List<Object> params = sqlCondition(cntSql,info);
		return crud.selectCount(cntSql.toString(),params.toArray());
	}
	
	/**
	 * 动态组合SQL语句的条件
	 * @param cntSql
	 * @param book
	 * @return
	 */
	private List<Object> sqlCondition(StringBuilder cntSql, OrderInfo info){
		List<Object> params = new ArrayList<Object>();
		StringBuilder whereSql = new StringBuilder(" where 1=1 and (");
		
		/**
		 *动态拼接语句：
		 */
		  int orderid = info.getOrderid();
		if(orderid>0) {
			whereSql.append(" or orderid like ? ");
			params.add("%" + orderid + "%");
		}
		int orderstatus = info.getOrderstatus();
		if(orderstatus>0) {
			whereSql.append("or orderstatus like ? ");
			params.add("%" + orderstatus + "%");
		}
		
		 String consignne = info.getConsignne();
		if(consignne != null && !consignne.trim().isEmpty()) {
			whereSql.append("or consignne like ? ");
			params.add("%" + consignne + "%");
		}
		whereSql.delete(16, 19).append(")");
		cntSql.append(whereSql.toString());
		return params;
	}
	/**
	 * 修改操作：
	 * @param info
	 * @param id
	 * @return
	 */

	public int updateorderinfo(OrderInfo info,String id) {
		Map<String,Object []> map=new LinkedHashMap<String,Object []> ();
		map.put("update order_info set consignne=?,goodsamount=?,orderamount=?,orderstatus=? where orderid=?", 
				new Object[]{info.getConsignne(),info.getGoodsamount(),info.getOrderamount(),info.getOrderstatus(),Integer.valueOf(id)});
		 return crud.updateAll(map)[0];
	}

	/**
	 * 详情操作
	 * @param info
	 * @return
	 */
	public List<OrderInfo> selectOneOrder(OrderInfo info) {
		// TODO Auto-generated method stub
		Map<String,Object []> map=new LinkedHashMap<String,Object []> ();
		map.put("select * from order_info where orderid=?", new Object[] {info.getOrderid()});
		return crud.selectAll(map).get(0);
	}
	/**
	 * 发货单操作：
	 * @param pc
	 * @param ps
	 * @return
	 */
	public Map<Integer, List<OrderInfo>> selectsendOrders(int pc, int ps) {
		Map<String,Object []> map=new LinkedHashMap<String,Object []> ();
		map.put("select * from order_info where shippingstatus='1' LIMIT ?,?",new Object[]{(pc-1)*ps,ps});
		return 	crud.selectAll(map);
	}

	/**
	 * 发货单操作：
	 * @return
	 */
	public int selectsendCount() {
		return crud.selectCount("select count(*) from order_info where shippingstatus='1'");
	}
	

	public List<OrderInfo> selectOrdersCountTime() {
		Map<String,Object []> map=new LinkedHashMap<String,Object []> ();
		map.put("select * from order_info",null);
		return crud.selectAll(map).get(0);
	}
	private static int dataCount(String data) {
		OrderInfoDao dao=new OrderInfoDao();
		List<OrderInfo> selectOrdersCountTime = dao.selectOrdersCountTime();
		int count=0;
	    for (OrderInfo orderInfo : selectOrdersCountTime) {
			if ((mydate(orderInfo.getAddtime())).equals(data)) {
				System.out.println(mydate(orderInfo.getAddtime()));
				count++;
			}
		}
		return count;
	}
	private static String mydate(int addtime) {
		Calendar cal = Calendar.getInstance();
		long millions = new Long(addtime).longValue() * 1000;
		cal.setTimeInMillis(millions);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(cal.getTime());
	}

}
